<?php

namespace app\api\controller;

use app\admin\model\BrowseLog;
use app\admin\model\BrowseSiteLog;
use app\admin\model\Navigation;
use app\admin\model\Notice;
use app\common\controller\Api;
use app\common\model\Ads;
use app\common\model\Category;
use GatewayWorker\Lib\Gateway;
use Obs\ObsClient;
use Obs\ObsException;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $typeArr = [23,24];
    protected $siteNavType = [92,93,94];
    protected $siteTypeArr = [58,63,65];
    protected $articleCategory = [25,30,36,46,53];
    protected $articleCategoryBanner = [132,133,134,135,136,137,138];


    public function push(){
        $url="http://api.hawkhawk.club/api/index/index?symbol=hf_CL";
        $res = juhecurl($url);
        $result=json_decode($res,true);
        $re = Gateway::sendToGroup(1,json_encode($result['data']));
        $this->success($re);
    }
    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function i8n()
    {
        $n = 10;

        $array = [];

        for ($i = 0;$i <= $n;$i++){
            $array[] = '测试用户'.$i;
        }
        halt(getLast($array));
    }

    /**
     * Notes: 图片合成
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/20 0020
     * Time: 13:32
     * @param $bigImgPath
     * @param $qCodePath
     * @param $dst_x
     * @param $dst_y
     * @param int $src_x
     * @param int $src_y
     */
    public function images($bigImgPath,$qCodePath,$dst_x,$dst_y,$src_x=0,$src_y=0){

        $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
        $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));

        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);

        imagecopymerge($bigImg, $qCodeImg, $dst_x, $dst_y, $src_x, $src_y, $qCodeWidth, $qCodeHight, 100);

        list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);

        imagejpeg($bigImg,PUBLIC_PATH.'/assets/img/qrcode1.jpg');
    }

    public function area(){
        $data = db('area_five')->group('town_id')->select();
        array_walk($data,function(&$t) {
            array_shift($t);
            array_pop($t);
            array_pop($t);
            db('area_four')->insert($t);
        });
        $this->success($data);
    }

    /**
     * Notes: banner 广告接口
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/21 0021
     * Time: 17:47
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bannerAds(){
        $category_id = input('category_id');
        $site_id = input('site_id');
        $where['site_id'] = 0;
        if(!empty($site_id)){
            $where['site_id'] = $site_id;
        }
        $where['category_id'] = $category_id;
        if(empty($category_id))$this->error('失败');
        $limit = 3;
        $adsModel = new \app\common\model\Ads();
        $data = $adsModel->where($where)->order('weigh')->limit($limit)->select();
        $this->success('成功',$data);
    }
    /**
     * Notes: 系统公告列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/21 0021
     * Time: 18:15
     * @throws \think\exception\DbException
     */
    public function notice(){
        $limit = input('limit',15);
        $noticeModel = new Notice();
        $site_id = input('site_id');
        $where = [];
        if(!empty($site_id)){
            $where['site_id'] = $site_id;
        }
        $where['status']=1;
        $notice = $noticeModel->where($where)->order('createtime desc')->paginate($limit);
        $this->success('成功',$notice);
    }

    public function newNotice(){
        $site_id = input('site_id');
        $where = [];
        if(!isInt($site_id,1)){
            $where['site_id'] = $site_id;
        }
        $where['status']=1;
        $noticeModel = new Notice();
        $notice = $noticeModel->where($where)->order('createtime desc')->find();
        $this->success('成功',$notice);
    }

    /**
     * Notes: 主站首页导航
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/25 0025
     * Time: 15:58
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function navigation(){
        $type = input('type');
        $site_id = input('site_id');
        if(!in_array($type,$this->typeArr)){
            $this->error('导航标识不正确');
        }
        $where = [];
        if(!empty($site_id)){
            $where['site_id'] = $site_id;
        }
        $where['status'] = 1;
        $categoryData = db('category')
            ->field('id,pid,name')
            ->where('pid',$type)
            ->where('status','normal')
            ->order('weigh')
            ->select();
        $categoryIds = array_column($categoryData,'id');
        $navigation = new Navigation();
        $where['category_id'] = ['in',$categoryIds];
        $navigationData = $navigation
            ->where($where)
            ->order('weigh')
            ->select();
        foreach ($categoryData as $kk=>$vv){
            $categoryData[$kk]['child'] =[];
            foreach ($navigationData as $k=>$v){
                if($vv['id']==$v['category_id']){
                    $categoryData[$kk]['child'][]=$v;
                }
            }
        }
        $this->success('成功',$categoryData);
    }
    public function footNavigation(){
        $where = [];
        $type = 81;
        $where['status'] = 1;
        $categoryData = db('category')
            ->field('id,pid,name')
            ->where('pid',$type)
            ->where('status','normal')
            ->order('weigh')
            ->select();
        $categoryIds = array_column($categoryData,'id');
        $navigation = new Navigation();
        $where['category_id'] = ['in',$categoryIds];
        $navigationData = $navigation
            ->where($where)
            ->order('weigh')
            ->select();
        foreach ($categoryData as $kk=>$vv){
            $categoryData[$kk]['child'] =[];
            foreach ($navigationData as $k=>$v){
                if($vv['id']==$v['category_id']){
                    $categoryData[$kk]['child'][]=$v;
                }
            }
        }
        $this->success('成功',$categoryData);
    }

    /**
     * Notes: 底部文章（废弃）
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/25 0025
     * Time: 16:56
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function footArticle(){
        $where = [];
        $type = 90;
        $where['status'] = 1;
        $navigation = new Navigation();
        $where['category_id'] = $type;
        $navigationData = $navigation
            ->where($where)
            ->order('weigh')
            ->limit(6)
            ->select();
        $this->success('成功',$navigationData);
    }

    /**
     * Notes: 分站导航 58,63,65
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/12 0012
     * Time: 17:04
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function siteNavigation(){
        $type = input('type');
        $site_id = input('site_id');
        if(!in_array($type,$this->siteTypeArr)){
            $this->error('导航标识不正确');
        }
        $where = [];
        if(!isInt($site_id,1))$this->error('缺少站点id');
        $where['site_id'] = $site_id;
        $where['status'] = 1;
        $categoryData = db('category')
            ->field('id,pid,name')
            ->where('pid',$type)
            ->where('site_id',$site_id)
            ->where('status','normal')
            ->order('weigh')
            ->select();
        $categoryIds = array_column($categoryData,'id');
        $navigation = new Navigation();
        $where['category_id'] = ['in',$categoryIds];
        $navigationData = $navigation
            ->where($where)
            ->order('weigh')
            ->select();
        foreach ($categoryData as $kk=>$vv){
            $categoryData[$kk]['child'] =[];
            foreach ($navigationData as $k=>$v){
                if($vv['id']==$v['category_id']){
                    $categoryData[$kk]['child'][]=$v;
                }
            }
        }
        $this->success('成功',$categoryData);
    }

    public function articleCategoryBanner(){
        $articleCategoryBanner = $this->articleCategoryBanner;
        $category = input('banner_category');
        $site_id = input('site_id');
        if(!in_array($category,$articleCategoryBanner))$this->error('参数有误');
        if(!isInt($site_id,1))$this->error('缺少参数');
        $ads = new Ads();
        $where['category_id'] = $category;
        $where['site_id'] = $site_id;
        $adsData = $ads
            ->where($where)
            ->order('weigh')
            ->select();
        if(empty($adsData)){
            $where['site_id'] = 0;
            $adsData = $ads
                ->where($where)
                ->order('weigh')
                ->select();
        }
        $this->success('成功',$adsData);
    }

    /**
     * Notes: 友情链接
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/25 0025
     * Time: 16:57
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function friendsLink(){
        $linkModel = new \app\common\model\Links();
        $linkData = $linkModel->where('status','1')->select();
        $this->success('成功',$linkData);
    }

    /**
     * Notes: 文章分类
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 17:06
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articleCategory(){
        $type = input('type');
        $site_id = input('site_id');
        if($type==25){
            if(!isInt($site_id,1))$this->error('请选择站点');
            $where['site_id'] = $site_id;
        }
        if(!in_array((int)$type,$this->articleCategory))$this->error('请传有效父级分类');
        $categoryModel = new Category();
        $where['pid'] = $type;
        $category = $categoryModel->field('id as category_id ,name,image')->where($where)->order('weigh', 'desc')->select();
        $this->success('成功',$category);
    }

    /**
     * Notes: 文章列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 17:52
     * @throws \think\exception\DbException
     */
    public function articleList(){
        $category_id = input('category_id');
        $keywords = input('keywords');
        $site_id = input('site_id');
        $limit = input('limit',15);
        $articleModel = new \app\admin\model\Article();
        $field = "a.*,c.name as category_name";
        $where =[];
        $where['a.site_id'] = 0;
        if(!empty($category_id))$where['a.category_id'] = $category_id;
        if(!empty($site_id))$where['a.site_id'] = $site_id;
        if(trim($keywords))$where['a.title'] = ['like','%'.trim($keywords).'%'];
        $where['a.publishdata'] = '2';
        $where['a.status'] = '1';
        $order = "a.createtime desc";
        $article = $articleModel
            ->alias('a')
            ->field($field)
            ->join('category c','c.id=a.category_id')
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $this->success('成功',$article);
    }

    /**
     * Notes: 基层站点搜索
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/12 0012
     * Time: 10:59
     */
    public function siteSearch(){
        $keywords = input('keywords');
        $limit = input('limit',15);
        //if(empty($keywords))$this->error('请输入搜索关键字');
        if($keywords){
            $where['sitename'] = ['like','%'.trim($keywords).'%'];
        }
        $where['is_siter'] = 1;
        $where['site_level'] = 5;
        $where['site_type'] = '2';
        $where['checkdata'] = '1';
        $where['status'] = 'normal';
        $where['is_check'] = '1';
        $site = Db::name('user')->field('id as site_id,sitename,site_level,site_number,area_str')->where($where)->paginate($limit);
        if($site)$this->success('成功',$site);
    }

    /**
     * Notes: 文章详情
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/7/10 0010
     * Time: 17:56
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function articleDetail(){
        $user_id = $this->request->param('user_id');
        $article_id = input('article_id');
        $url = input('url');
        if(empty($article_id))$this->error('缺少参数');
        $articleModel = new \app\admin\model\Article();
        $detail = $articleModel->where('id',$article_id)->where('status','1')->find();
        if($detail['publishdata']==1)$this->error('无此文章');
        Db::name('article')->where('id',$article_id)->setInc('reading');
        if($user_id){
            $browseLogModel = new BrowseLog();
            $log = $browseLogModel->where('article_id',$article_id)->where('user_id',$user_id)->find();
            if(empty($log)){
                $logData=[
                    'user_id' => $user_id,
                    'article_id'=>$article_id
                ];
                if($url){
                    $logData['url'] = $url;
                }
                $browseLogModel->save($logData);
            }else{
                $browseLogModel->where('article_id',$article_id)->where('user_id',$user_id)->setField('createtime',time());
            }

        }
        $this->success('成功',$detail);
    }

    public function survey(){
        $site_id = input('site_id');
        $data = [];
        $whereArticle = [];
        if($site_id){
            $whereArticle['site_id'] = $site_id;
            $whereArticle['category_id'] = 76;
            $where['site_id']=$site_id;
        }else{
            $whereArticle['site_id'] = 0;
            $whereArticle['category_id'] = 76;
            $where['site_id']=0;
        }
        $where['publishdata'] = '1';
        $where['status'] = '1';
        $articleModel = new \app\admin\model\Article();
        $data['hot'] = $articleModel->where('status','1')->where($whereArticle)->where('publishdata','2')->order('createtime desc')->find();
        $data['campaign'] = Db::name('vote')->where('typedata','1')->where($where)->order('createtime desc')->find();
        $data['vote'] = Db::name('vote')->where('typedata','2')->where($where)->order('createtime desc')->find();
        $data['active'] = Db::name('vote')->where('typedata','3')->where($where)->order('createtime desc')->find();
        $this->success('成功',$data);
    }

    public function tipOffWay(){
        $site_id = input('site_id');
        $where = [];
        if($site_id){
            $where['site_id'] = $site_id;
        }
        $where['status'] = '1';
        $where['category_id'] = 71;
        $navigationModel = new Navigation();
        $navigation = $navigationModel->where($where)->select();
        $this->success('成功',$navigation);
    }

    public function siteConfig(){
        $site = \think\Config::get("site");
        $this->success('成功',[
            'android_download_qrcode'=>$site['file_url'].$site['android_download_qrcode'],
            'ios_download_qrcode'=>$site['file_url'].$site['ios_download_qrcode'],
            'wxgzh_download_qrcode'=>$site['file_url'].$site['wxgzh_download_qrcode'],
            'beian'=>$site['beian'],
            'name'=>$site['name'],
            'site_mobile'=>$site['site_mobile'],
            'jianjie'=>$site['jianjie'],
            'contact'=>explode(',',$site['contact'])
        ]);
    }

    public function siteNav(){
        $type = input('type');
        if(!in_array($type,$this->siteNavType)){
            $this->error('导航标识不正确');
        }
        $navigation = new Navigation();
        $where['category_id'] = $type;
        $where['status'] = 1;
        $navigationData = $navigation
            ->where($where)
            ->order('weigh')
            ->select();
        $this->success('成功',$navigationData);
    }

    public function special(){
        $site_id = input('site_id',0);
        $category = (new Category())->field('id,name,image,site_id')->where('pid','=',97)->where('site_id',$site_id)->order('weigh desc')->find();
        if(empty($category))$this->error('未设置专题','0');
        $this->success('成功',$category);
    }
    
}
